package com.archermind.style.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.archermind.style.bean.StyleType;
import com.archermind.style.interceptor.Auth;
import com.archermind.style.pojo.ElementEnum;
import com.archermind.style.service.OperationService;
import com.archermind.style.service.StyleTypeService;
import com.archermind.style.util.Result;
import com.archermind.style.util.Search;
import com.feng.fengDUtil.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *@Author 丰建立
 *@Date 2019/1/25 16:43
 * ["查看样式分类","编辑样式分类"]
 */

@RestController
@RequestMapping(value = "/styleType")
public class StyleTypeAction {

    @Autowired
    private StyleTypeService styleTypeService;

    @Autowired
    private OperationService opService;


    @RequestMapping(value = "/elementEnum")
    public Result elementList() {
       JSONArray list = ElementEnum.getDict();
       return Result.success(list);
    }

    @Auth("查看样式分类")
    @RequestMapping(value = "/list")
    public Result listIconType(Page page, Search search) {
        styleTypeService.query(page,search);
        return Result.success(page);
    }

    @RequestMapping(value = "/listByElement")
    public Result styleTypeList(@RequestParam(value = "element") ElementEnum element, Integer instanceId) {
        List<StyleType> list = styleTypeService.listByElement(element,instanceId);
        return Result.success(list);
    }

    @RequestMapping(value = "/instanceStyleEdit")
    public Result instanceStyleEdit(String field,String fieldVal,Integer styleTypeId, Integer instanceId) {
         styleTypeService.updateInstanceStyle( field, fieldVal, styleTypeId,  instanceId);
        return Result.success();
    }


    @Auth("编辑样式分类")
    @RequestMapping(value="/save")
    public Result save(@RequestBody StyleType styleType){
        try {
            styleTypeService.saveOrUpdate(styleType);
            opService.saveLog("添加了样式分类："+styleType.getName());
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Auth("编辑样式分类")
    @RequestMapping(value="/del")
    public Result del(Integer styleTypeId){
        try {
            StyleType styleType = styleTypeService.selectById(StyleType.class,styleTypeId);
            styleTypeService.delete(styleType);
            opService.saveLog("删除了样式分类："+styleType.getName());
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping(value="/checkUniqueName_mute")
    public JSONObject checkUniqueName(@RequestParam(value = "id",required =false ) Integer id,
                                      @RequestParam(value = "name",required = true) String name,
                                      @RequestParam(value = "element",required = true) String element){
        JSONObject ret = new JSONObject();
        ret.put("isUnique",styleTypeService.isUnique(name,id,element));


        return ret;
    }


    @RequestMapping(value="/checkUniqueText_mute")
    public JSONObject checkUniqueText_mute(@RequestParam(value = "id",required =false ) Integer id,
                                      @RequestParam(value = "text",required = true) String text,
                                      @RequestParam(value = "element",required = true) String element){
        JSONObject ret = new JSONObject();
        ret.put("isUnique",styleTypeService.isUniqueText(text,id,element));

        return ret;
    }


}